{
Traviesadora beta 0.2.0.13.33 (30.11.2008)
LastMod: 18.12.2008 by Prof1983

Copyright 2008, travieorama@googlemail.com

This file is part of the taviesadora project

taviesadora is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

taviesadora is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
}
unit CreateAccountWin;

interface

uses
  Buttons, Classes, Controls, Dialogs, ExtCtrls, Forms, Graphics, Messages, StdCtrls, StrUtils, SysUtils, Variants, Windows,
  IdBaseComponent, IdComponent, IdHTTP, IdTCPClient, IdTCPConnection,
  XMLTag, XMLParser, LoadJabberServer, JabberHelper, ProgressPanel;

type
  TfrmCreateAccount = class(TForm)
    edtPW1: TLabeledEdit;
    edtPW2: TLabeledEdit;
    edtUN: TLabeledEdit;
    cmbServers: TComboBox;
    imgredball: TImage;
    imgYellBall: TImage;
    ImgGreenBall: TImage;
    edtServer: TEdit;
    sbtnDropDown: TSpeedButton;
    Label1: TLabel;
    lblServer: TLabel;
    ImgInvPW1: TImage;
    ImgInvPW2: TImage;
    LoadServersButton: TButton;
    procedure cmbServersDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
    procedure sbtnDropDownClick(Sender: TObject);
    procedure cmbServersSelect(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure edtUNChange(Sender: TObject);
    procedure edtPW2Change(Sender: TObject);
    procedure edtServerChange(Sender: TObject);
    procedure LoadServersButtonClick(Sender: TObject);
  private
    btnOK: TButton;
    btnCancel: TButton;
    FProgressPanel: TProgressPanel;
  private
    JabberServerLoader: TLoadJabberServer;
    ThreadStopped: Boolean;
    procedure ServersReady(Sender: TObject);
    procedure CheckInputs();
  protected
    procedure DoCreate(); override;
  public
    constructor Init(UserName: string; Server: string; Password: string);
    procedure LoadServers();
  end;

implementation

uses
  {Profile,} DebugWin;

{$R *.dfm}

{ TfrmCreateAccount }

constructor TfrmCreateAccount.Init(UserName, Server, Password: string);
begin
  JabberServerLoader := TLoadJabberServer.Create();
  JabberServerLoader.OnTerminate := ServersReady;
  ThreadStopped := False;
  edtUN.Text := UserName;
  edtServer.Text := Server;
  edtPW1.Text := Password;
  //LoadServers();
end;

procedure TfrmCreateAccount.cmbServersDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  s: string;
  p: string;
  rating: Integer;
  i: Integer;
  Graphic: TGraphic;
begin
  with cmbServers.Canvas do
  begin
    brush.Style := bsSolid;
    pen.Style := psClear;

    if (state = [odSelected]) or (state = [odFocused]) or (state = [odFocused,odSelected]) then
    begin
      Brush.Color := $00CECEF4
    end
    else
    begin
      Brush.Color := clWindow;
    end;

    Rectangle(rect.Left,Rect.Top,Rect.Right+1,Rect.Bottom+1);
    font.Color := $00252558;
  end;
  s := cmbServers.Items[index];
  if (Pos(#9,s) > 0) then
  begin
    p := s;
    Delete(p,1,pos(#9,p));
    Delete(s,pos(#9,s),length(s));
    with cmbServers.Canvas do
    begin
      TextOut(rect.left+2,rect.Top,s);
      delete(p,pos('%',p),1);
      Rating := StrToIntDef(p,1);
      if (Rating > 90) then
      begin
        Graphic := ImgGreenBall.Picture.Graphic;
        rating := round(3*(rating-90)/10)+1;
      end
      else if (Rating >= 80) then
      begin
        Graphic := imgYellBall.Picture.Graphic;
        rating := Round(3*(rating-80)/10)+1;
      end
      else
      begin
        Graphic := imgredball.Picture.Graphic;
        rating := round(3*rating/80)+1;
      end;
      for i := 0 to rating do
      begin
        Draw(rect.Right-i*16,Rect.Top-1,Graphic);
      end;
      //TextOut(rect.Right-canvas.TextWidth(p),rect.Top,p);
    end;
  end
  else
  begin
    canvas.TextOut(rect.left+2,rect.Top,s);
  end;
end;

procedure TfrmCreateAccount.sbtnDropDownClick(Sender: TObject);
begin
  cmbServers.DroppedDown := True;
  cmbServers.Visible := True;
end;

procedure TfrmCreateAccount.cmbServersSelect(Sender: TObject);
begin
  edtServer.Text := LeftStr(cmbServers.Items[cmbServers.itemindex],pos(#9,cmbServers.Items[cmbServers.itemindex])-1);
end;

procedure TfrmCreateAccount.LoadServers();
begin
  FProgressPanel.Text := 'Retrieving serverlist..';
  FProgressPanel.Visible := True;
  JabberServerLoader.Resume();
  sbtnDropDown.Enabled := False;

  while not(ThreadStopped) do
  begin
    FProgressPanel.Step();
    Application.ProcessMessages();
    Sleep(10);
  end;
end;

procedure TfrmCreateAccount.ServersReady(Sender: TObject);
begin
  FProgressPanel.Visible := False;
  cmbServers.Items.AddStrings(JabberServerLoader.ServerList);
  ThreadStopped := True;
  sbtnDropDown.Enabled := true;
  cmbServers.Visible := false;
end;

procedure TfrmCreateAccount.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if not(ThreadStopped) then
  begin
    JabberServerLoader.Terminate;
    repeat
      Application.ProcessMessages;
    until ThreadStopped;
  end;
end;

procedure TfrmCreateAccount.edtUNChange(Sender: TObject);
begin
  CheckInputs;
  edtUN.EditLabel.Caption := 'Username';
end;

procedure TfrmCreateAccount.edtPW2Change(Sender: TObject);
begin
  CheckInputs;
end;

procedure TfrmCreateAccount.CheckInputs;
begin
  {
  btnOK.Enabled := (edtUN.Text <> '') and (edtPW1.Text = edtPW2.Text) and (edtPW1.Text <> '') and (edtServer.Text <> '');
  btnOK.Repaint;
  ImgInvPW1.Visible := edtPW1.Text = '';
  ImgInvPW2.Visible := edtPW1.Text <> edtPW2.Text;
  }
end;

procedure TfrmCreateAccount.edtServerChange(Sender: TObject);
begin
  lblServer.Caption := 'Server';
end;

procedure TfrmCreateAccount.DoCreate();
begin
  inherited;
  ThreadStopped := True;

  btnOK := TButton.Create(Self);
  btnOK.Parent := Self;
  btnOK.Left := 168;
  btnOK.Top := 136;
  btnOK.Width := 75;
  btnOK.Height := 25;
  btnOK.Caption := 'OK';
  btnOK.Font.Charset := DEFAULT_CHARSET;
  btnOK.Font.Color := 2434392;
  btnOK.Font.Height := -11;
  btnOK.Font.Name := 'MS Sans Serif';
  btnOK.Font.Style := [];
  btnOK.TabOrder := 0;
  btnOK.TabStop := True;
  btnOK.ModalResult := mrOk;

  btnCancel := TButton.Create(Self);
  btnCancel.Parent := Self;
  btnCancel.Left := 248;
  btnCancel.Top := 136;
  btnCancel.Width := 75;
  btnCancel.Height := 25;
  btnCancel.Caption := 'Cancel';
  btnCancel.Font.Charset := DEFAULT_CHARSET;
  btnCancel.Font.Color := 2434392;
  btnCancel.Font.Height := -11;
  btnCancel.Font.Name := 'MS Sans Serif';
  btnCancel.Font.Style := [];
  btnCancel.TabOrder := 1;
  btnCancel.TabStop := True;
  btnCancel.ModalResult := mrCancel;

  FProgressPanel := TProgressPanel.Create(Self);
  FProgressPanel.Left := 162;
  FProgressPanel.Top := 48;
  FProgressPanel.Width := 265;
  FProgressPanel.Height := 41;
end;

procedure TfrmCreateAccount.LoadServersButtonClick(Sender: TObject);
begin
  LoadServers();
end;

end.
